//
//  AppealDetailViewController.m
//  Utils
//
//  Created by enghou on 2018/6/23.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "AppealDetailViewController.h"
#import "FileItemModel.h"
#import "NetWorkerManager.h"
#import "MSSBrowseNetworkViewController.h"
#import "AudioViewController.h"
#import "VideoViewController.h"
@interface AppealDetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *eventNameLbl;
@property (weak, nonatomic) IBOutlet UILabel *orgNameLbl;
@property (weak, nonatomic) IBOutlet UILabel *occurPlaceLbl;
@property (weak, nonatomic) IBOutlet UILabel *occurTimeLbl;
@property (weak, nonatomic) IBOutlet UILabel *mainipulatorLbl;
@property (weak, nonatomic) IBOutlet UILabel *phoneLbl;
@property (weak, nonatomic) IBOutlet UILabel *appealSourceLbl;
@property (weak, nonatomic) IBOutlet UILabel *appealTypeLbl;
@property (weak, nonatomic) IBOutlet UILabel *appealDtl;

@property(nonatomic,strong)NSArray *files;
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *btns;
@end

@implementation AppealDetailViewController

-(instancetype)init{
    return [[UIStoryboard storyboardWithName:@"AppealDetail" bundle:[NSBundle mainBundle]]instantiateInitialViewController];
}

-(void)showImageWithImageURLS:(NSArray *)urls currentIdx:(NSInteger)idx{
    NSMutableArray *images = [NSMutableArray array];
    NSInteger rightIdx = idx;
    for (FileItemModel *model in urls) {
        NSInteger index = [urls indexOfObject:model];
        if (model.type == Image) {
            [images addObject:[NSString stringWithFormat:@"%@/%@",ServerPath,model.path]];
        }else{
            if (index > idx) {
                rightIdx --;
            }
        }
    }
    NSMutableArray *models = [NSMutableArray array];
    for (NSString *path in images) {
        MSSBrowseModel *model = [[MSSBrowseModel alloc]init];
        model.bigImageUrl = path;
        [models addObject:model];
    }
    MSSBrowseNetworkViewController *browse = [[MSSBrowseNetworkViewController alloc]initWithBrowseItemArray:models currentIndex:rightIdx];
    
    [browse showBrowseViewController];
    
}

-(void)showAudioWithFile:(FileItemModel *)file{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ServerPath,file.path]];
    AudioViewController *audio = [[AudioViewController alloc]init];
    audio.fileURL = url;
    [self presentVC:audio];
}

-(void)showVideoWithFile:(FileItemModel *)file{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ServerPath,file.path]];
    VideoViewController *audio = [[VideoViewController alloc]init];
    audio.url = url;
    [self presentVC:audio];
}

- (IBAction)showFileAction:(id)sender {
    if (self.files.count > 0) {
        FileItemModel *model = self.files[0];
        switch (model.type) {
            case Image:
                [self showImageWithImageURLS:self.files currentIdx:0];
                break;
            case Audio:
                [self showAudioWithFile:self.files[0]];
                break;
            case Video:
                [self showVideoWithFile:self.files[0]];
                break;
            default:
                break;
        }
    }
}

- (IBAction)showMiddleFileAction:(id)sender {
    if (self.files.count > 1) {
        FileItemModel *model = self.files[1];
        switch (model.type) {
            case Image:
                [self showImageWithImageURLS:self.files currentIdx:1];
                break;
            case Audio:
                [self showAudioWithFile:self.files[1]];
                break;
            case Video:
                [self showVideoWithFile:self.files[1]];
                break;
            default:
                break;
        }
    }
}

- (IBAction)showLastFileAction:(id)sender {
    if (self.files.count > 2) {
        FileItemModel *model = self.files[2];
        switch (model.type) {
            case Image:
                [self showImageWithImageURLS:self.files currentIdx:2];
                break;
            case Audio:
                [self showAudioWithFile:self.files[2]];
                break;
            case Video:
                [self showVideoWithFile:self.files[2]];
                break;
            default:
                break;
        }
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/appeal/getAppealDetailsById.action" parameter:@{@"appealId":self.model.ids} completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            NSDictionary *data = source[@"data"];
            self.eventNameLbl.text = data[@"name"];
            self.orgNameLbl.text = data[@"org"][@"orgName"];
            self.occurPlaceLbl.text = data[@"occurrencePlace"];
            self.occurTimeLbl.text = data[@"occurrenceTime"];
            self.mainipulatorLbl.text = data[@"litigant"];
            self.phoneLbl.text = data[@"telphone"];
            self.appealSourceLbl.text = data[@"appealSource"][@"appealSource"];
            self.appealTypeLbl.text = [NSString stringWithFormat:@"%@-%@",data[@"appealType"][@"domainName"],data[@"appealType"][@"dictName"]];
            self.appealDtl.text = data[@"description"];
            NSArray *files = data[@"files"];
            NSMutableArray *arr = [NSMutableArray array];
            for (NSDictionary *element in files) {
                FileItemModel *model = [[FileItemModel alloc]initWithSource:element];
                [arr addObject:model];
            }
            self.files = arr;
            [self.files enumerateObjectsUsingBlock:^(FileItemModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.type == Image) {
                    UIImageView *btn = self.btns[idx];
                    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ServerPath,obj.path]];
                    [btn sd_setImageWithURL:url];
                }else if (obj.type == Video){
                    UIImageView *btn = self.btns[idx];
                    btn.image = [UIImage imageNamed:@"icon_video"];
                }else if (obj.type == Audio){
                    UIImageView *btn = self.btns[idx];
                    btn.image = [UIImage imageNamed:@"icon_audio"];
                }
            }];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 8) {
        NSString *str = self.appealDtl.text;
        CGSize size = [str caculateSizeWithFont:[UIFont systemFontOfSize:17] MaxSize:CGSizeMake(ScreenWidth - 86, CGFLOAT_MAX)];
        return size.height > 10 ? size.height + 44 : 44;
    }else{
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
